package com.zq.ssmdemo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;
@PropertySource("classpath:jdbc.properties")
public class MybatisConfig {
    @Value("${jdbc.driver}")
    private String driver="com.mysql.jdbc.Driver";
    @Value("${jdbc.url}")
    private String url="jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8";
    @Value("${jdbc.username}")
    private String username="root";
    @Value("${jdbc.password}")
    private String password="123456";

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSourcess = new DruidDataSource();
        dataSourcess.setDriverClassName(driver);
        dataSourcess.setUrl(url);

        dataSourcess.setUsername(username);
        dataSourcess.setPassword(password);
        return dataSourcess;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean =  new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        //指定mybatis配置文件
        ClassPathResource classPathResource = new ClassPathResource("mybatis-config.xml");
        sqlSessionFactoryBean.setConfigLocation(classPathResource);
        SqlSessionFactory object = sqlSessionFactoryBean.getObject();
        return object;
    }



    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.zq.ssmdemo.mapper");
        return mapperScannerConfigurer;
    }



}
